﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace gui
{
    class Config
    {
         /// <summary>
        /// IP
        /// </summary>
        public string Ip { set; get; }

        /// <summary>
        /// Порт
        /// </summary>
        public int Port { set; get; }

        /// <summary>
        /// Логин пользователя
        /// </summary>
        public string Login { set; get; }

        public string TypeConnection {set; get; }

        public string DataBaseName { set; get; }

        public string DataBaseTable { set; get; }

        public string ComputerName { set; get; }

        /// <summary>
        /// XML документ
        /// </summary>
        private XDocument _xmlDocument;

        public Config() 
        {
           
        }

        /// <summary>
        /// Получение настроек
        /// </summary>
        /// <param name="filename">Имя файла</param>
        public void GetConfig()
        {
            ClientSettings settings = new ClientSettings();
            this._xmlDocument = XDocument.Load(settings.FileName);
            var query = (from data in this._xmlDocument.Descendants(settings.MainElement)
                         select new Config()
                         {
                             Ip = data.Element(settings.IpElement).Value,
                             Port = System.Convert.ToInt32(data.Element(settings.PortElement).Value),
                             Login = data.Element(settings.LoginElement).Value,
                             TypeConnection = data.Element(settings.TypeConnection).Value,
                             DataBaseName = data.Element("DataBaseName").Value,
                             DataBaseTable = data.Element("DataBaseTable").Value,
                             ComputerName = data.Element("ComputerName").Value
                         }).First();

            this.Ip = query.Ip;
            this.Port = query.Port;
            this.Login = query.Login;
            this.TypeConnection = query.TypeConnection;
            this.DataBaseName = query.DataBaseName;
            this.DataBaseTable = query.DataBaseTable;
            this.ComputerName = query.ComputerName;
        }
    }
}
